package com.yexiao.cofe.proxy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class RabbitMQProxy {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 消息过期事件，单位：毫秒
    private final Long expireTime = 5 * 1000L; // 默认5秒

    private final String exchange = "exchange.coffee";
    private final String routingKey = "business.routingkey";

    public void sendMessage(Long orderId) {
        rabbitTemplate.convertAndSend(exchange, routingKey, orderId, new MessagePostProcessor() {

            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration(String.valueOf(expireTime));
                return message;
            }
        });
        log.info("订单过期——定时消息发送：订单号：{}", orderId);
    }
}
